package com.hjwoo.app.views;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.been.QueryContact;
import com.hjwoo.app.been.UserAccessTokenOBJ;
import com.hjwoo.app.common.LoanApplication;
import com.hjwoo.app.fragment.UserCenterFragment;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.utils.LoanAccessTokenIdKeeper;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;

public class UserCenterActivity extends BaseAppCompatActivity implements RequestListener {

    public static final int REQUEST_CODE_01 = 1;
    private Gson gson = new Gson();
    private TextView usercenter_cellphone_et, usercenter_name_tv, usercenter_city_tv, usercenter_managername_et, usercenter_managercellphone_et;
    private EditText usercenter_idnumber_et, usercenter_cardinfo_et, usercenter_cardno_et;
    private LinearLayout usercenter_city_ll;
    private String c;
    private QueryContact queryContact;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usercenter);
        SpannableString ss = new SpannableString("dd");
        if (getIntent() != null) {
            queryContact = (QueryContact) getIntent().getSerializableExtra("queryContact");
        }
        initView();
    }

    void initView() {

        Utils.initTitle("个人中心", this, true);

        usercenter_name_tv = (TextView) findViewById(R.id.usercenter_name_tv);
        usercenter_city_tv = (TextView) findViewById(R.id.usercenter_city_tv);
        c = usercenter_city_tv.getText().toString();
        usercenter_managername_et = (TextView) findViewById(R.id.usercenter_managername_et);
        usercenter_managercellphone_et = (TextView) findViewById(R.id.usercenter_managercellphone_et);

        usercenter_cellphone_et = (TextView) findViewById(R.id.usercenter_cellphone_et);
        usercenter_idnumber_et = (EditText) findViewById(R.id.usercenter_idnumber_et);
        usercenter_cardinfo_et = (EditText) findViewById(R.id.usercenter_cardinfo_et);
        usercenter_cardno_et = (EditText) findViewById(R.id.usercenter_cardno_et);

        usercenter_city_ll = (LinearLayout) findViewById(R.id.usercenter_city_ll);

        usercenter_name_tv.setText(queryContact.getFullName());
        usercenter_city_tv.setText(queryContact.getCity());
        usercenter_managername_et.setText(queryContact.getUserName());
        usercenter_managercellphone_et.setText(queryContact.getUserCode());

        usercenter_cellphone_et.setText(queryContact.getCellphone());
        usercenter_idnumber_et.setText(queryContact.getIdNumber());
        usercenter_cardinfo_et.setText(queryContact.getBankName());
        usercenter_cardno_et.setText(queryContact.getBankAccount());

        usercenter_managercellphone_et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);

                //url:统一资源定位符
                //uri:统一资源标示符（更广）
                intent.setData(Uri.parse("tel:" + usercenter_managercellphone_et.getText().toString()));
                //开启系统拨号器
                startActivity(intent);
            }
        });

        //朝向列表
        usercenter_city_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle b = new Bundle();
                b.putSerializable("ls", new String[]{
                        "北京", "上海", "成都", "青岛", "济南", "郑州", "南京", "西安"
                });
                Intent i = new Intent(UserCenterActivity.this, SelectListActivity.class);
                i.putExtras(b);
                c = usercenter_city_tv.getText().toString();
                i.putExtra("equalStr", c);
                startActivityForResult(i, REQUEST_CODE_01);
            }
        });

        findViewById(R.id.usercenter_commit_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserAccessTokenOBJ userAccessTokenOBJ = LoanAccessTokenIdKeeper.readAccessToken(LoanApplication.getInstance());
//                提交
                RequestMethod.getMethod().appUpdateContact(userAccessTokenOBJ.getAppSessionId(), usercenter_city_tv.getText().toString(),
                        usercenter_idnumber_et.getText().toString(), usercenter_cardinfo_et.getText().toString(), usercenter_cardno_et.getText().toString(), UserCenterActivity.this, UserCenterActivity.this);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //先判断是哪个页面返回过来的
        switch (resultCode) {
            case RESULT_OK:
                String cityName = data.getStringExtra("itemName");
                usercenter_city_tv.setText(cityName);
                c = cityName;
                break;
        }
    }

    @Override
    public void requestSuccess(String tag, String message) {
        if (tag.equals(RequestTag.APPUPDATECONTACT)) {
//            提交成功
            LoanToast.showToastShort("提交成功", UserCenterActivity.this);
            Intent intent = new Intent();
            intent.putExtra("msg", message);
            UserCenterActivity.this.setResult(UserCenterFragment.USERINFO_RESULT_CODE, intent);
            finish();
        }
    }

    @Override
    public void requestError(String message) {
        FailureResultObject failureResultObject = new FailureResultObject();
        failureResultObject = gson.fromJson(message, FailureResultObject.class);
        LoanToast.showToastShort(failureResultObject.getErrorMessage(), UserCenterActivity.this);
    }
}